package com.uls.web.controller.hdh;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.uls.hdh.domain.HdhOptions;
import com.uls.hdh.domain.HdhPolls;
import com.uls.hdh.domain.HdhVotes;
import com.uls.hdh.service.IHdhOptionsService;
import com.uls.hdh.service.IHdhPollsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.hdh.domain.HdhPollResults;
import com.uls.hdh.service.IHdhPollResultsService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;

/**
 * 投票统计Controller
 * 
 * @author hdh
 * @date 2024-11-11
 */
@RestController
@RequestMapping("/hdh/results")
public class HdhPollResultsController extends BaseController
{
    @Autowired
    private IHdhPollResultsService hdhPollResultsService;
    @Autowired
    private IHdhOptionsService hdhOptionsService;

    @Autowired
    private IHdhPollsService hdhPollsService;
    /**
     * 查询投票统计列表
     */
    @PreAuthorize("@ss.hasPermi('hdh:results:list')")
    @GetMapping("/list")
    public TableDataInfo list(HdhPollResults hdhPollResults)
    {
        startPage();
        List<HdhPollResults> list = hdhPollResultsService.selectHdhPollResultsList(hdhPollResults);
        for (HdhPollResults hdhPollResults1:list){
            Long pollId=hdhPollResults1.getPollId();
            HdhPolls hdhPolls=hdhPollsService.selectHdhPollsByPollId(pollId);
            if(hdhPolls!=null){
                hdhPollResults1.setTitle(hdhPolls.getTitle());
            }
        }
        for (HdhPollResults hdhPollResults1:list){
            Long optionId=hdhPollResults1.getOptionId();
            HdhOptions hdhOptions=hdhOptionsService.selectHdhOptionsByOptionId(optionId);
            if(hdhOptions!=null){
                hdhPollResults1.setOptionText(hdhOptions.getOptionText());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出投票统计列表
     */
    @PreAuthorize("@ss.hasPermi('hdh:results:export')")
    @Log(title = "投票统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HdhPollResults hdhPollResults)
    {
        List<HdhPollResults> list = hdhPollResultsService.selectHdhPollResultsList(hdhPollResults);
        ExcelUtil<HdhPollResults> util = new ExcelUtil<HdhPollResults>(HdhPollResults.class);
        util.exportExcel(response, list, "投票统计数据");
    }

    /**
     * 获取投票统计详细信息
     */
    @PreAuthorize("@ss.hasPermi('hdh:results:query')")
    @GetMapping(value = "/{resultId}")
    public AjaxResult getInfo(@PathVariable("resultId") Long resultId)
    {
        return success(hdhPollResultsService.selectHdhPollResultsByResultId(resultId));
    }

    /**
     * 新增投票统计
     */
    @PreAuthorize("@ss.hasPermi('hdh:results:add')")
    @Log(title = "投票统计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HdhPollResults hdhPollResults)
    {
        hdhPollResults.setCreatedBy(this.getUsername());
        hdhPollResults.setCreatedTime(new Date());
        return toAjax(hdhPollResultsService.insertHdhPollResults(hdhPollResults));
    }

    /**
     * 修改投票统计
     */
    @PreAuthorize("@ss.hasPermi('hdh:results:edit')")
    @Log(title = "投票统计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HdhPollResults hdhPollResults)
    {
        hdhPollResults.setUpdatedBy(this.getUsername());
        hdhPollResults.setUpdatedTime(new Date());
        return toAjax(hdhPollResultsService.updateHdhPollResults(hdhPollResults));
    }

    /**
     * 删除投票统计
     */
    @PreAuthorize("@ss.hasPermi('hdh:results:remove')")
    @Log(title = "投票统计", businessType = BusinessType.DELETE)
	@DeleteMapping("/{resultIds}")
    public AjaxResult remove(@PathVariable Long[] resultIds)
    {
        return toAjax(hdhPollResultsService.deleteHdhPollResultsByResultIds(resultIds));
    }
}
